/******************************************************************************* 
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation
 ******************************************************************************/
package net.sourceforge.perforce.internal.api;

import junit.framework.Assert;

import org.eclipse.core.runtime.IProgressMonitor;

/**
  * @version $Revision: 119 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
class TestProgressMonitor implements IProgressMonitor 
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the total work amount. */
	private int totalWork = -1;
	/** Stores the really worked amount. */
	private int worked = -1;
	/** Indicates whether done has been called. */
	private boolean doneCalled;
	
	// ------------------------------------------------------------------------------ Public Methods

	/** @see IProgressMonitor#beginTask(java.lang.String, int) */
	public void beginTask(String name, int newTotalWork) 
	{
		if (totalWork == -1)
		{	
			totalWork = newTotalWork;
			worked = 0;
		}
		else
		{
			Assert.fail("IProgressMonitor.beginTask() may only be called once.");
		}
	}

	/** @see IProgressMonitor#done() */
	public void done() 
	{
		doneCalled = true;
	}

	/** @see IProgressMonitor#internalWorked(double) */
	public void internalWorked(double work) 
	{
	}

	/** @see IProgressMonitor#isCanceled() */
	public boolean isCanceled() 
	{
		return false;
	}

	/** @see IProgressMonitor#setCanceled(boolean) */
	public void setCanceled(boolean value) 
	{
	}

	/** @see IProgressMonitor#setTaskName(java.lang.String) */
	public void setTaskName(String name) 
	{
	}

	/** @see IProgressMonitor#subTask(java.lang.String) */
	public void subTask(String name) 
	{
	}

	/** @see IProgressMonitor#worked(int) */
	public void worked(int work) 
	{
		if (totalWork == -1)
		{
			Assert.fail("IProgressMonitor.beginTask() has to be called first.");
		}
		
		worked += work;
	}
	
	/** Called to verify the progress monitor at the end of the operation. */
	public void verify()
	{
		Assert.assertEquals(totalWork, worked);
		Assert.assertTrue("IProgressMonitor.done() wasn't called", totalWork == -1 || doneCalled);
	}
}
